const logService = require('../services/logService')
const logger = require('../utils/logger')

// 获取日志列表
exports.getList = async (req, res) => {
  try {
    const { page, size, type, startTime, endTime } = req.query
    
    const data = await logService.getLogs({
      page: parseInt(page) || 1,
      size: parseInt(size) || 10,
      type,
      startTime,
      endTime
    })

    res.json({
      code: 200,
      data
    })
  } catch (error) {
    logger.error('获取日志列表失败:', error)
    res.status(500).json({
      code: 500,
      message: '获取日志列表失败'
    })
  }
}

// 删除日志
exports.delete = async (req, res) => {
  try {
    const { ids } = req.body
    if (!ids || !Array.isArray(ids)) {
      return res.status(400).json({
        code: 400,
        message: '参数错误'
      })
    }

    await logService.deleteLogs(ids)

    res.json({
      code: 200,
      message: '删除成功'
    })
  } catch (error) {
    logger.error('删除日志失败:', error)
    res.status(500).json({
      code: 500,
      message: '删除日志失败'
    })
  }
}

// 创建日志
exports.create = async (req, res) => {
  try {
    const log = await logService.createLog(req.body)
    
    res.json({
      code: 200,
      data: log,
      message: '创建成功'
    })
  } catch (error) {
    logger.error('创建日志失败:', error)
    res.status(500).json({
      code: 500,
      message: '创建日志失败'
    })
  }
} 